package hot.pay.office.controller;

import com.sunmnet.framework.controller.BaseController;
import com.sunmnet.framework.model.JsonResult;
import hot.pay.office.model.entity.Customer;
import hot.pay.office.service.ICustomerService;
import hot.pay.office.service.IReadExcelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/customer")
@Api(tags = "客户信息")
public class  CustomerController extends BaseController {

	@Autowired
	private ICustomerService customerService;




	@ApiOperation(value = "保存客户信息")
	@ResponseBody
	@RequestMapping(value = "/save", method = {RequestMethod.POST})
	@ApiImplicitParams({
			@ApiImplicitParam(name = "customer",value = "客户信息实体类",required = true, paramType = "body", dataType = "Customer")
	})
	public JsonResult<Integer> save(Customer customer) {
		return buildSuccJson(customerService.save(customer));
	}

	@ApiOperation(value = "获取客户信息")
	@ResponseBody
	@RequestMapping(value = "/get", method = {RequestMethod.POST})
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id",value = "客户ID",dataType = "Long", paramType = "query")
	})
	public JsonResult<Customer> get( Long id) {
		return buildSuccJson(customerService.get(id));
	}


	@ApiOperation(value = "导入客户excel文件")
	@ResponseBody
	@RequestMapping(value = "/import", method = {RequestMethod.POST})
	@ApiImplicitParams({
			@ApiImplicitParam(name = "file",value = "excel文件",dataType = "MultipartFile", paramType = "query")
	})
	public JsonResult<Boolean> importCustomer(MultipartFile file) throws Exception {
		byte[] bytes = file.getBytes();
		String UPLOADED_FOLDER = "upload/";
		Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
		Files.write(path, bytes);
		return buildSuccJson(customerService.importCustom(path.toString()));
	}



}
